﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using Caliburn.Micro;
using Agnes.Core;
using Agnes.Libaray;
using Agnes.Core;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// 新建子目录命令
    /// </summary>
    public class NewSubCatalogCmd
    {
        private static NewSubCatalogCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static NewSubCatalogCmd Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new NewSubCatalogCmd();
                return _instance;
            }
        }

        private RoutedCommand _requery;

        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        NewSubCatalogCmd()
        {
            CanExcuted = true;
            _requery = new RoutedCommand("New Article ParentCatalog", typeof(NewSubCatalogCmd));
        }

        /// <summary>
        /// 新建目录的路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }

        /// <summary>
        /// 新建目录执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            Catalog newCatalog = Catalog.CreateAsChild();

            //如果选中的节点为空,那么将建立到首层目录下
            if (ArticleManager.Instance.SelectedNode == null)
            {
                newCatalog.Title = "新建目录";
                ArticleManager.Instance.Roots.Add(newCatalog);
                AgnesServiceManager.LogServ.Info("新建目录命令: 新建‘"+newCatalog.Title+"在‘我的知识库/’路径下");
            }
            else
            {
                //否则将目录加到选中的目录下
                newCatalog.Title = "新建目录";
                (ArticleManager.Instance.SelectedNode as Catalog).Childs.Add(newCatalog);
                newCatalog.ParentCatalog = (ArticleManager.Instance.SelectedNode as Catalog);
                AgnesServiceManager.LogServ.Info("New Sub ParentCatalog Cmd: Add A New ParentCatalog: " + (ArticleManager.Instance.SelectedNode as Catalog).Title
                    + " in: " + newCatalog.GetPath());
            }

            ArticleManager.Instance.Save();
        }


        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (AgnesServiceManager.AgnesSlnService.CurrentSln == null)
                e.CanExecute = false;
            else
                e.CanExecute = CanExcuted;
        }
    }
}
